/**
 * Project: metacrumbs
 * File: AbstractAttachment.java
 * Type: AbstractAttachment
 * Author: unmarco
 * Created: Sep 7, 2011 12:51:31 AM
 */
package org.sensolab.metacrumbs.data.model;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;

/**
 * @author unmarco
 *
 */
@PersistenceCapable
public class Attachment implements AttachedContent {

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
    @Persistent
	private String title;
	
	@Persistent
	private Key location;
	
	@Persistent
	private String mimeType;
	
	@Persistent
	private Blob content;
	
	public Attachment() { }
	
	public Attachment(Key location, String title) {
		this.title = title;
		this.location = location;
		this.mimeType = null;
		this.content = null;
	}
	
	public Attachment(Key location, String title, String mimeType, Blob content) {
		this.location = location;
		this.title = title;
		this.mimeType = mimeType;
		this.content = content;
	}
	
	public Attachment(Key location, String title, String mimeType, byte[] content) {
		this.location = location;
		this.title = title;
		this.mimeType = mimeType;
		this.content = new Blob(content);
	}
	
	/**
	 * @return
	 */
	public Key getKey() {
		return key;
	}
	
	/**
	 * @return
	 */
	public Key getLocation() {
		return location;
	}

	/**
	 * @param location
	 */
	public void setLocation(Key location) {
		this.location = location;
	}

	/**
	 * @return title of this attachment
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * @param title title of this attachment
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/* (non-Javadoc)
	 * @see org.sensolab.metacrumbs.data.model.AttachedContent#getMimeType()
	 */
	@Override
	public String getMimeType() {
		return mimeType;
	}

	/**
	 * @param mimeType the mime type of this attachment's content
	 */
	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}
	
	/* (non-Javadoc)
	 * @see org.sensolab.metacrumbs.data.model.AttachedContent#getContent()
	 */
	@Override
	public Blob getContent() {
		return content;
	}

	/**
	 * @param content
	 */
	public void setContent(Blob content) {
		this.content = content;
	}
	
	/**
	 * @param content
	 */
	public void setContent(byte[] content) {
		this.content = new Blob(content);
	}

	/**
	 * @return
	 */
	public byte[] getBytes() {
		return content.getBytes();
	}

}
